package com.im.server.special.business.service;

import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.CopyOnWriteArraySet;

import javax.annotation.Resource;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.im.server.general.business.message.data.LoginData;
import com.im.server.general.business.message.data.UserData;
import com.im.server.general.business.push.PersonalPush;
import com.im.server.general.business.thread.SessionServerHandler;
import com.im.server.general.common.bean.User;
import com.im.server.general.common.bean.UserHead;
import com.im.server.general.common.dao.UserDAO;
import com.im.server.general.common.dao.UserHeadDAO;
import com.im.server.general.common.message.ResultMessage;
import com.im.server.general.common.service.api.UserBaseService;
import com.only.net.session.SocketSession;

/**
 * 描述： 个人业务处理service
 * 
 * @author XiaHui
 * @date 2016年1月7日 下午7:48:52
 * @version 0.0.1
 */
@Service
@Transactional
public class SpecialPersonalService {

	@Resource
	private UserDAO userDAO;
	@Resource
	private UserHeadDAO userHeadDAO;
	@Resource
	private UserBaseService userBaseService;
	@Resource
	SessionServerHandler sessionServerHandler;
	@Resource
	PersonalPush personalPush;
	@Resource
	LdapHandler ldapHandler;

	/**
	 * 登陆
	 * 
	 * @param userMessage
	 * @return
	 */
	public ResultMessage login(SocketSession socketSession, LoginData loginData) {
		ResultMessage message = new ResultMessage();
		if (null != loginData) {
			String account = loginData.getAccount();
			if (StringUtils.isNotBlank(account)) {
				account = account.replace(" ", "").replace("'", "");
				account = ldapHandler.handleAccount(account);
				User user = userDAO.getByAccount(account);
				if (null != user) {
					String loginPassword=loginData.getPassword();
					if (StringUtils.isNotBlank(loginData.getPassword())) {
						//loginPassword=OnlyMD5Util.md5L32(loginPassword);
						if (ldapHandler.auth(account, loginPassword)) {
							
							UserHead userHead=userHeadDAO.getLastByUserId(user.getId());
							UserData userData = new UserData();
							BeanUtils.copyProperties(user, userData);
							message.put("userData", userData);
							message.put("userHead", userHead);
							socketSession.setAuth(true);
							socketSession.setKey(user.getId());
							putSocketSession(socketSession);
							userBaseService.putUserStatus(user.getId(), loginData.getStatus());
						} else {
							message.addWarning("0010001", "密码错误！");
						}
					} else {
						message.addWarning("0010001", "密码不能为空！");
					}
				} else {
					message.addWarning("0010001", "帐号不存在！");
				}
			} else {
				message.addWarning("0010001", "帐号不能为空！");
			}
		} else {
			message.addWarning("0010001", "帐号和密码不能为空！");
		}
		return message;
	}

	private void putSocketSession(SocketSession socketSession) {
		String key = socketSession.getKey();
		if (sessionServerHandler.isSingleSession()) {
			CopyOnWriteArraySet<SocketSession> set = sessionServerHandler.remove(key);
			if (set != null && !set.isEmpty()) {
				Map<String, Object> client = new HashMap<String, Object>();
				client.put("clientType", "");
				client.put("clientVersion", "");
				client.put("onlineTime", "");

				personalPush.pushOtherOnline(set, client, true);
			}
		}
		sessionServerHandler.put(socketSession.getKey(), socketSession);
	}
}
